home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Using char array in functions
- Date: 21 Feb 1996 14:44:33 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gg78hINNg0j@keats.ugrad.cs.ubc.ca>
- References: <20FEB199609155775@sundog.caltech.edu>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <20FEB199609155775@sundog.caltech.edu>,
- STUART F. TAYLOR (818)395-3807, MC 264-33 CALTECH PASADENA CA 91125 <taylor@sundog.caltech.edu> wrote:
- >I've searched the faq's and postings, but still am looking for a clear
- >explanation on how to use arrays of char strings in functions, especially when
- >I want to have the function modify the array. I find I can't just pass the
- >name of the string as a pointer as I do for single strings.
-
- An array of strings is basically an array of character pointer objects, which
- hold the addresses of (named or unnamed) zero-terminated character arrays.
-
- You can directly pass an array of strings to a function, in which you can
- modify or examine any of the pointers.
-
- Here is a piece of trivia: A data structure consisting of a
- vector of vector ``descriptors'' is called an ``Iliffe vector'',
- after its inventor, John Iliffe. In this case, the descriptors
- are pointers, and vectors are basically arrays.
-
- Here is how you declare a string Iliffe vector, which contains pointers to
- static, unnamed literal strings:
-
- char *strarray[] = { "Foo", "Bar" };
-
- This means that ``strarray'' is an array of objects of type ``char *''.
- There is a missing size, so the compiler will obtain the size by counting the
- number of initializers. The strarray[0] will hold string ``Foo'' by storing a
- pointer to it, and strarray[1] will hold string ``Bar''.
-
- The C language does not specify the conventions for representing the size of a
- string array. One way you can do it is by adopting a convention, throughout
- your program, that the last element of a string array will be a null pointer:
-
- char *strarray[] = { "Foo", "Bar", 0 };
-
- That allows any function to find the end of your Iliffe Vector just by looking
- for a null pointer. You can also get the size of such an array using
- sizeof(strarray), but this does not work inside a function. A function which
- receives an array of strings can be defined like this:
-
- int strarylen(char *array[])
- {
- /* look for the null pointer, and report array size */
- int i;
- for (i = 0; array[i] != 0; i++)
- ;
- return i;
- }
-
- Alternatively, the "char *array[]" parameter can be declared as "char **array".
- In the declarations of function parameters, the two are considered equivalent:
-
- If a parameter is declared to have type ``array of <type>,''
- the declaration is adjusted to read ``pointer to <type>;''
- (K&R2 A10.1)
-
- Either way, the size of the referenced array is not known inside the function,
- so you rely on the presence of the zero pointer at the end, or you have to pass
- the size around as an extra argument. There is no dictated convention; the
- representation you choose is up to you.
-
- You could, for instance, define a structure which holds arbitrary string arrays
- (via a pointer to the first char * in the array, and also has the length:
-
- struct striliffe {
- char **vector;
- int size;
- };
-
- Once you start doing heavy string manipulation, you will probably start using
- dynamic allocation, and will have to worry about where strings come from.
-
- It's possible to have an array of strings, some of which are static, modifiable
- arrays, some of which are unmodifiable string literals and others which come
- from dynamic storage! You can't tell them apart---they are all pointers to
- zero-terminated character arrays. Try to avoid causing a mess like that!
- --
-
-